extends "res://tscn/book/tab.gd"


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
var showTag = false 

func initOptBtns(re = false):
	opt1.setTagName('Mod')
	opt4.setTagName('出生地')
	opt1.setItems(tag1List,re)
	opt4.setItems(tag4List,re)
	if sys.main.ui.has_node("bookDlg"):
		chaNum.show()
	else:
		chaNum.hide()

func init():
	if isShow : 
		reInit()
		return
	box.set_columns(1)
	isShow = true
	if name == "单位" :
		var dlg = sys.main.ui.get_node("bookDlg")
		for i in data.getList("c") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			var ori = item.getOrientation()
			addOri(ori,item) 
		
			var origin = item.origin
			if item.lock == 1 and sys.main.ui.has_node("bookDlg"):
				if item.origin.find("城镇") > -1:
					dlg.townNum += 1
					dlg.upTown()
				elif item.origin.find("地牢") > -1:
					dlg.dilaoNum += 1
					dlg.upDilao()
			
			var prof = item.getProfession()
			var modName = utils.getModNameFromData(item)
			
			if not tag1List.has(modName):
				tag1List.append(modName)
			if not tag2List.has(ori):
				tag2List.append(ori)
			if not tag3List.has(prof):
				tag3List.append(prof)
			if not tag4List.has(origin):
				tag4List.append(origin)
	sortBox()	

func reInit():
	var childs = box.get_children()
	for c in childs:
		var hasVisbleCha = reSetChaVisible(c)
		c.set_visible(hasVisbleCha)
		c._layout()
	resetTagList()
			
func reSetChaVisible(chasPan):
	var chasBtn = chasPan.box.get_children()
	var visibleNum = 0
	for cbtn in chasBtn:
		var c = cbtn.cha 
		var canShow = true 
		if selectTag4 and selectTag4 != c.origin:
			canShow = false 
			
		if selectTag3 and selectTag3 != c.getProfession():
			canShow = false 
		
		if selectTag2 and selectTag2 != c.getOrientation():
			canShow = false 
		
		if selectTag1 and selectTag1 != utils.getModNameFromData(c):
			canShow = false
			
		if findName != null and canShow:
			for text in findName.split(" "):
				if text and text.length() > 0:
					var regex = RegEx.new()
					regex.compile(".*" + text +".*")
					var result1 = regex.search(c.name)
					var result2 = regex.search(c.tab)
					canShow = canShow and (is_instance_valid(result1) or is_instance_valid(result2) ) 
			
		cbtn.set_visible(canShow)
		if canShow:	visibleNum = visibleNum + 1 
	return visibleNum > 0
			
func resetTagList():
	if not showTag : return 
	var newTagList2 = []
	var newTagList3 = []
	
	for j in box.get_children():
		for i in j.box.get_children() :
			var item = i.cha
			var ori = item.getOrientation()
			var prof = item.getProfession()
			var orign = item.origin
			var modName = utils.getModNameFromData(item)
			if selectTag1 and  modName != selectTag1:
				continue
				
			if not newTagList2.has(ori):
				newTagList2.append(ori)
				
			if not newTagList3.has(prof):
				newTagList3.append(prof)
				
				
#	opt2.setItems(newTagList2,true)
#	opt3.setItems(newTagList3,true)
		
func sortBox():
	for j in box.get_children():
		j.sortCha()
	
	
func addProf():
	pass #原版函數保護下

func addOri(ori,cha):
	for i in box.get_children():
		if i.ori == ori :
			i.addCha(cha)
			return
	var pan = preload("res://tscn/book/chasPan.tscn").instance()
	box.add_child(pan)
	pan.init(ori)
	pan.addCha(cha)

func SetAllDisable(bl) :
	var dlg = sys.main.ui.get_node("bookDlg")
	for j in box.get_children():
		if not j.is_visible():continue
		for i in j.box.get_children() :
			if i.has_node("lock") and i.is_visible():
				var lock = i.get_node("lock")
				var item = lock.item
				if lock.get_node("box/2").pressed:
					if item.origin.find("城镇") > -1:
						dlg.townNum -= 1
						dlg.upTown()
					elif item.origin.find("地牢") > -1:
						dlg.dilaoNum -= 1
						dlg.upDilao()
					lock.setDisable(true)


